//% color="#ff9f06" iconWidth=50 iconHeight=40
namespace Tracker{


    //% block="巡线模块" blockType="tag"
    export function tagtest1() {}

    
    //% block="Initialize  line tracker at I2C address 0x7C" blockType="command"  
    export function Tracker_init(parameter: any, block: any) { 
        Generator.addImport('from pinpong.board import Board,Pin');
        Generator.addImport('from pinpong.libs.dfrobot_box_line_sensor import *');
        Generator.addInit(`board_begin`,`Board().begin()`);
        Generator.addInit(`Tracker_object`,`LS = Box_LineSensor(0x30)`);
   
    }


    //% block="Calibrate line tracker sensor [PROBE] threshold to [THRESHOLD_SET]" blockType="command" 
    //% PROBE.shadow="dropdown" PROBE.options="PROBE"
    //% THRESHOLD_SET.shadow="range" THRESHOLD_SET.params.min=0 THRESHOLD_SET.params.max=255 THRESHOLD_SET.defl=150
    export function THRESHOLD_SET(parameter: any, block: any) { 
        let probe=parameter.PROBE.code;
        let threshold_set=parameter.THRESHOLD_SET.code;

        Generator.addCode(`LS.set_threshod_number(${probe}, ${threshold_set})`);

    }


    //% block="Get all line sensor thresholds(array)" blockType="reporter" 
    export function THRESHOLD_GET(parameter: any, block: any) { 

        Generator.addCode(`LS.get_threshod_value()`);

    }


    //% block="Read all probe analog values(array)" blockType="reporter" 
    export function Tracker_read_all_analog(parameter: any, block: any) { 

        Generator.addCode(`LS.get_adc_value()`);

    }


    //% block="Read all probe digital values(string)" blockType="reporter" 
    export function Tracker_read_all_digital(parameter: any, block: any) { 

        Generator.addCode(`LS.get_adc_all()`);

    }


    //% block="Read all digital probe values equal [VALUE]?" blockType="boolean" 
    //% VALUE.shadow="string" VALUE.defl="00000"
    export function Tracker_read_all_boolean(parameter: any, block: any) { 
        
        let value=parameter.VALUE.code;
        Generator.addCode(`LS.get_adc_all() == ${value}`);
    }


    //% block="Read digital value of line sensor [PROBE]" blockType="boolean"
    //% PROBE.shadow="dropdown" PROBE.options="PROBE"
    export function Tracker_read_digital(parameter: any, block: any) { 
        
        let probe=parameter.PROBE.code;
        Generator.addCode(`LS.get_adc(${probe})`);
    }



    //% block="电机控制" blockType="tag"
    export function tagtest2() {}

    //% block="Motor [MOTOR] [TURN] at [SPEED]" blockType="command"  
    //% MOTOR.shadow="dropdown" MOTOR.options="MOTOR" 
    //% TURN.shadow="dropdown" TURN.options="TURN" 
    //% SPEED.shadow="range" SPEED.params.min=0 SPEED.params.max=1023 SPEED.defl=800
    export function MOTORrotation(parameter: any, block: any) { 
        let motor=parameter.MOTOR.code;
        let turn=parameter.TURN.code;
        let speed=parameter.SPEED.code;


        Generator.addImport('from pinpong.board import Board,Pin');
        Generator.addImport('from pinpong.extension.unihiker import *');
        Generator.addInit(`board_begin`,`Board().begin()`);

        if (motor == "M1"){
            Generator.addInit(`M1_veer`,`M1_veer = Pin(Pin.P5, Pin.OUT)`);
            Generator.addInit(`M1_speed`,`M1_speed = Pin(Pin.P8, Pin.PWM)`);
            Generator.addCode(`M1_veer.write_digital(${turn})`);
            Generator.addCode(`M1_speed.write_analog(${speed})`);
        }
        else if (motor == "M2"){
            Generator.addInit(`M2_veer`,`M2_veer = Pin(Pin.P6, Pin.OUT)`);
            Generator.addInit(`M2_speed`,`M2_speed = Pin(Pin.P16, Pin.PWM)`);
            Generator.addCode(`M2_veer.write_digital(${turn})`);
            Generator.addCode(`M2_speed.write_analog(${speed})`);
        }
        else{
            Generator.addInit(`M1_veer`,`M1_veer = Pin(Pin.P5, Pin.OUT)`);
            Generator.addInit(`M1_speed`,`M1_speed = Pin(Pin.P8, Pin.PWM)`);
            Generator.addInit(`M2_veer`,`M2_veer = Pin(Pin.P6, Pin.OUT)`);
            Generator.addInit(`M2_speed`,`M2_speed = Pin(Pin.P16, Pin.PWM)`);
            Generator.addCode(`M1_veer.write_digital(${turn})`);
            Generator.addCode(`M1_speed.write_analog(${speed})`);
            Generator.addCode(`M2_veer.write_digital(${turn})`);
            Generator.addCode(`M2_speed.write_analog(${speed})`);
        }
        
    }

    //% block="Motor [MOTOR] stop" blockType="command"  
    //% MOTOR.shadow="dropdown" MOTOR.options="MOTOR" 
    export function MOTORstop(parameter: any, block: any) { 
        let motor=parameter.MOTOR.code;

        Generator.addImport('from pinpong.board import Board,Pin');
        Generator.addImport('from pinpong.extension.unihiker import *');
        Generator.addInit(`board_begin`,`Board().begin()`);

        if (motor == "M1"){
            Generator.addInit(`M1_speed`,`M1_speed = Pin(Pin.P8, Pin.PWM)`);
            Generator.addCode(`M1_speed.write_analog(0)`);
        }
        else if(motor == "M2"){
            Generator.addInit(`M2_speed`,`M2_speed = Pin(Pin.P16, Pin.PWM)`);
            Generator.addCode(`M2_speed.write_analog(0)`);
        }
        else{
            Generator.addInit(`M1_speed`,`M1_speed = Pin(Pin.P8, Pin.PWM)`);
            Generator.addInit(`M2_speed`,`M2_speed = Pin(Pin.P16, Pin.PWM)`);
            Generator.addCode(`M1_speed.write_analog(0)`);
            Generator.addCode(`M2_speed.write_analog(0)`);
        }
   
    }



}

